<?php
/**
 *
 * @author Rodrigo Régis Palmeira <regisbsb@gmail.com>
 *
 */
namespace Foxforms\Widget\Field;
class Checkbox extends \Foxforms\Widget\Field
{
	protected $value;
	protected $default;

	public static function create($name)
	{
		return new self($name);
	}

	public function setValue($value)
	{
		$this->value = $value;
		return $this;
	}

	public function defaultValue($value)
	{
		$this->default	= $value;
		return $this;
	}

	public function getValue()
	{
		return $this->value;
	}

	public function render()
	{
		$html	= ' ';
		$html	.= "<input type='checkbox'";

		if (!empty($this->name))
			$html	.= " name='{$this->name}'";
		if (!empty($this->default))
			$html	.= " value='{$this->default}'";
		if (!empty($this->readOnly))
			$html	.= " readonly";
		if (!empty($this->disabled))
			$html	.= " disabled='{$this->disabled}'";
		if (!empty($this->id))
			$html	.= " id='{$this->id}{$key}'";
		if ($this->value == $this->default)
			$html	.= " checked";

		if (count($this->class))
		{
			$class = implode(' ',$this->class);
			$html.= " class='{$class}'";
		}

		if (count($this->style))
		{
			$style = implode('; ',$this->style);
			$html.= " style=\"{$style}\"";
		}

		if (count($this->event))
		{
			foreach ($this->event as $event => $action)
			{
				$action = implode(';',$action);
				$html.= " {$event}=\"{$action}\"";
			}
		}

		$html	.= " />";

		return $html;
	}
}